<?php
/*
 * @美潮风
 * @功能说明：明星机构模块
 * @更新说明：暂无更新
 * @文件名 HospitalController.class.php
 * @编码 UTF-8
 * @创建时间 2016-9-18 下午16:21:25
 * @创建人 yanyan
 */
namespace home\Controller;

use Think\Controller;
class HospitalController extends ApibaseController
{
    /*
     * 铜人宝-医院管理
     * @date: 2018-1-22 下午16:21:25
     * @editor: yanyan
     */
    public function index() {
        /*参数初始化*/
        $page = I('get.page') ?: 1;
        $perpage = 10;
        $url = array();
        $newsModel = M('Hospital');
        if (I('get.hos_name')) {
            $newsModel = $newsModel->where('hos_name LIKE "%' . I('get.hos_name') . '%"');
            $url['hos_name'] = I('get.hos_name');
        }
        $custObject = clone $newsModel;
        $newsList = $newsModel->order(array( 'add_at' => 'desc'))->page($page, $perpage)->select();
        $this->assign('page', setPage($custObject->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('newsList', $newsList);
        $this->display('hospital/index');
    }

    public function addHospt(){
        if(I('post.dosubmit')){
            $exists = M('Hospital')->where(['main_code'=>trim(I('post.main_code'))])->find();
            if($exists){
                $this->showtips('医院编码已存在');
            }
//            var_dump(I('post.'));exit;
            $logo = '';
            $str = I('post.logo');
            if($str{0} == '/'){
                $logo = uploadQiniu(UPLOAD_PATH.I('post.logo'),'hos_logo');
            }
            $data = [
                'hos_name'  => trim(I('post.hos_name')),
                'main_code'  => trim(I('post.main_code')),
                'hos_type'  => trim(I('post.hos_type')),
                'contact_person'  => trim(I('post.name')),
                'mobile'  => trim(I('post.mobile')),
                'email'  => trim(I('post.email')),
                'logo'  => $logo,
                'salt'  => randstr(5),
                'agency_number'  => trim(I('post.agency_number')),
                'province'  => trim(I('post.s_province')),
                'introduce'=> trim(I('post.hospt_detail')),
                'city'  => trim(I('post.s_city')),
                'update'    =>time()
            ];
            if(I('post.id')){
                $update = M('Hospital')->where(['id'=>intval(I('post.id'))])->setField($data);
                if($update){
                    $this->showtips('修改成功');
                }
            }
            $data['add_at'] = time();
            if(M('Hospital')->add($data)){
                $this->showtips('添加成功');
            }
        }elseif(I('get.id')){
            $getData = M('Hospital')->where(['id'=>intval(I('get.id'))])->find();
            $this->assign('getData',$getData);
        }
        $this->display('hospital/addhospt');
    }

}